// eslint-disable-next-line prettier/prettier
import * as Utils from '@/utils';

class ProjectEnv {
  constructor() {
    this.envConfig = {
      prod: 'https://thunders.com.hk/manage',
    };
    this.envData = {
      dev: {
        env: 'dev',
        apiPath: 'http://localhost:8001',
      },
      prod: {
        env: 'prod',
        apiPath: 'https://thunders.com.hk/website',
        loginUrl: '',
      },
    };
  }
  initEnvData() {
    const origin = location.origin;
    if (origin.indexOf('localhost') >= 0) {
      sessionStorage.setItem('est_env', Utils.encrypt(JSON.stringify(this.envData['dev'])));
      return false;
    }
    const pathnameArr = location.pathname.split('/');
    const path1 = `${origin}/${pathnameArr[1]}`;
    const path2 = `${origin}/${pathnameArr[1]}/${pathnameArr[2]}`;
    const p1 = Object.values(this.envConfig).indexOf(path1);
    const p2 = Object.values(this.envConfig).indexOf(path2);
    console.log('环境:::', p1, p2, Object.keys(this.envConfig)[p1 < 0 ? p2 : p1]);
    sessionStorage.setItem(
      'est_env',
      Utils.encrypt(
        JSON.stringify(this.envData[Object.keys(this.envConfig)[p1 < 0 ? p2 : p1]] || null),
      ),
    );
  }
  getEnv() {
    const est_env = sessionStorage.getItem('est_env');
    return est_env ? Utils.decrypt(est_env) : '';
  }
}

export default new ProjectEnv();
